package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Dynamic;
import cn.wolfcode.trip.base.domain.DynamicComment;
import cn.wolfcode.trip.base.query.DynamicQueryObject;
import cn.wolfcode.trip.base.service.IDynamicCommentService;
import cn.wolfcode.trip.base.service.IDynamicService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 推荐游记资源控制器
 */
@RestController
@RequestMapping("dynamics")
public class DynamicController {
    @Autowired
    private IDynamicService dynamicService;
    @Autowired
    private IDynamicCommentService dynamicCommentService;


    /**
     * 根据用户id查询朋友圈动态
     * 1.资源   /dynamics
     * 2.请求方式   GET
     * 3.请求参数   id
     * 4.返回结果   Dynamic对象
     */
    @GetMapping("{userId}")
    public PageInfo selectContentById(DynamicQueryObject qo){
        return dynamicService.query(qo);
    }

    /**
     * 发布朋友圈
     *
     */
    @PostMapping
    public JsonResult saveOrUpdateComment(Dynamic dynamic){
        JsonResult jsonResult = new JsonResult();
        try {
            dynamicService.save(dynamic);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("发布失败,请稍后重试...");
        }
        return jsonResult;
    }
    /**
     * 点赞朋友圈
     *
     */
    @PostMapping("{dynamicId}/dynamicGoods")
    public JsonResult like(@PathVariable Long dynamicId){
        JsonResult jsonResult = new JsonResult();
        try {
            dynamicService.like(dynamicId);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("发布失败,请稍后重试...");
        }
        return jsonResult;
    }

    /**
     *添加朋友圈评论
     * @param
     * @return
     */
    @PostMapping("{dynamic.id}/dynamicComments/-1")
    public JsonResult comment(DynamicComment dynamicComment){
        JsonResult jsonResult = new JsonResult();
        try {
            dynamicCommentService.save(dynamicComment);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("发布失败,请稍后重试...");
        }
        return jsonResult;
    }
    /**
     *添加朋友圈评论回复
     * @param
     * @return
     */
    @PostMapping("{dynamic.id}/dynamicComments/{reply.id}")
    public JsonResult reply(DynamicComment dynamicComment){
        JsonResult jsonResult = new JsonResult();
        try {
            dynamicCommentService.save(dynamicComment);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.mark("发布失败,请稍后重试...");
        }
        return jsonResult;
    }

}
